package com.affectu.user.action;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.jboss.seam.annotations.Name;

import com.affectu.dao.DAOBase;
import com.affectu.user.Role;

/**
 * Oct 17, 2008
 * @author daniel nathan
 */
@Name("roleConverter")
public class RoleConverter extends DAOBase implements Converter {

	/* (non-Javadoc)
	 * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.String)
	 */
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
		Object obj=new Object();
		try{
			obj=em.createQuery("from Role r where r.name=:name").setParameter("name", value).uniqueResult();
		}catch(Exception ex){
			this.logAffectuException(ex, "");
		}
		return obj;
	}

	/* (non-Javadoc)
	 * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
	 */
	public String getAsString(FacesContext arg0, UIComponent arg1, Object obj) {
		String value="";
		try{
			value=((Role)obj).getName();
		}catch(Exception ex){
			this.logAffectuException(ex, "");
		}
		return value;
	}

}
